//
//  SLSessionDownloadTaskController.m
//  Objective-C
//
//  Created by S_LINE on 2019/9/7.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLSessionDownloadTaskController.h"

#import "SLDownloadTaskManager.h"
#import "SLDownloadDataManager.h"
#import "SLDownloadMissionManager.h"
#import "SLDownloadViewModel.h"

#import "SLDownloadCell.h"

#import <Masonry.h>

@interface SLSessionDownloadTaskController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) SLDownloadViewModel *viewModel;

@property (nonatomic, weak) UITableView *tableView;

@end

@implementation SLSessionDownloadTaskController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [SLDownloadMissionManager shareInstance];
    
    [self initNavBar];
    
    [self initData];
    
    [self initView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterBackgroundNotification) name:@"enterBackground" object:nil];

}

- (void)appWillEnterBackgroundNotification {
    NSLog(@"appWillEnterBackgroundNotification");
}

- (void)initNavBar {
    self.navTitle = @"SessionDownloadTask";
    
    self.rightTitle = @"全部开始";
    
    [SLDownloadDataManager shareInstance];
}

- (void)navNext {
    if ([@"全部开始" isEqualToString:self.rightTitle]) {
        [[SLDownloadMissionManager shareInstance] startAllTasks];
        self.rightTitle = @"全部暂停";
    }
    else {
        [[SLDownloadMissionManager shareInstance] pauseAllTasksToDownloadStateEnum:SLDownloadStatePaused];
        self.rightTitle = @"全部开始";
    }
}

- (void)initData {
    @weakify(self);
    self.viewModel = [[SLDownloadViewModel alloc] init];
    self.viewModel.reloadAction = ^(NSIndexPath * _Nonnull indexPath) {
        @strongify(self);
        if (!self || !self.tableView) {
            return;
        }
        
        SLDownloadCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        if ([cell isKindOfClass:[SLDownloadCell class]]) {
            cell.fileModel = [self.viewModel dataForRowAtIndexPath:indexPath];
        }
    };
}

- (void)initView {
    UITableView *tableView = [[UITableView alloc] init];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.tableFooterView = [[UIView alloc] init];
    [tableView registerClass:[SLDownloadCell class] forCellReuseIdentifier:NSStringFromClass([SLDownloadCell class])];
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark - UITableView's delegate & datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger numberOfSections = [self.viewModel numberOfSections];
    NSLog(@"numberOfSectionsInTableView: %ld", numberOfSections);
    return numberOfSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger numberOfRowsInSection = [self.viewModel numberOfRowsInSection:section];
    NSLog(@"numberOfRowsInSection section: %ld - numberOfRows: %ld", section, numberOfRowsInSection);
    return numberOfRowsInSection;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SLDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SLDownloadCell class])];
    
    SLDownloadFileModel *fileModel = [self.viewModel dataForRowAtIndexPath:indexPath];
    cell.fileModel = fileModel;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SLDownloadFileModel *fileModel = [self.viewModel dataForRowAtIndexPath:indexPath];
    
    return [fileModel cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    SLDownloadFileModel *fileModel = [self.viewModel dataForRowAtIndexPath:indexPath];
    if (fileModel.downloadStateEnum == SLDownloadStateDownloading) {
        [[SLDownloadMissionManager shareInstance] pauseDownloadWithFileModel:fileModel toDownloadStateEnum:SLDownloadStatePaused];
    }
    else {
        [[SLDownloadTaskManager shareInstance] startDownloadWithFileModel:fileModel];
    }
}

@end
